<?php
/**
 * Created by PhpStorm.
 * User: xiong <1099446868@qq.com>
 * DateTime: 2018/11/10
 * TIME: 15:01
 */

namespace app\admin\controller;

use app\admin\model\Article as ArticleModel;
use think\Console;
use think\Db;
use think\facade\Request;

class Article extends BaseController {

    public function alist($class = 'php', $page = 1) {
        $article = ArticleModel::getArticleByClass($class, $page);
        //dump($article);
        $this->assign('article', $article);

        return $this->fetch();
    }

    //导数据   http://itbus.com/x/article/dao/wx/1.html
    public function dao($class = '', $page = 1) {
        exit;
        $return = ArticleModel::dao($class, $page);
        return $return == true ? '第 ' . $page . ' 页数据导入完成' : '失败';
    }

    //导数据2
    public function dao2($class = 'php', $page = 1) {
        exit;
        $return = ArticleModel::dao2($class, $page);
        //dump($return);
        return $return == true ? '第 ' . $page . ' 页数据导入完成至' . $return : '第' . $return . '条开始失败';
    }

    //导数据 url   http://itbus.com/x/article/dao3/32.html
    public function dao3($page) {
        exit;
        $id = ArticleModel::dao4($page);
        if ($id < 62670) {
            $nextpage = $page + 1;
            $msg = "第{$page}页数据导入完成，正在导入第{$nextpage}页数据,最后一条数据id是：" . $id;
            $this->success($msg, url('/x/article/dao3/' . $nextpage), '', 1);
        } else {
            return '数据全部导入完成';
        }
    }

    //将采集来的思否数据导入   http://itbus.com/x/dao/java_new/2.html
    public function dao4($class, $page, $currentTableCount = 0, $newTableCount = 0) {
        exit('需要继续完善');
        $id = ArticleModel::dao5($class, $page);

        if($currentTableCount == 0){
            $currentTableCount = ArticleModel::getArticleCount();
        }
        if($newTableCount == 0){
            $newTableCount = ArticleModel::newTableCount($class);
        }

        if ($id < $currentTableCount + $newTableCount) {   //原纪录 + 新纪录  61865
            $nextpage = $page + 1;

            $msg = "第{$page}页数据导入完成，正在导入第{$nextpage}页数据,最后一条数据id是：" . $id;
            $this->success($msg, url('/x/dao/' . $class .'/'. $nextpage .'/'.$currentTableCount .'/'. $newTableCount), '', 1);
        } else if($id == 'unknow error'){
            return '数据全部导入完成';
        }
    }

    //book_bookname 添加新字段并设置值ctable
    public function ctable($page){
        $table = $page * 2000;
        $nextnum = $table + 2000;
        $sql = "update book_bookname set ctable=" . $table . " where id >= {$table} and id < {$nextnum}";
        Db::execute($sql);
        $sql2 = "CREATE TABLE `book_con_{$table}` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `bid` int(11) DEFAULT NULL,
          `bookname` varchar(20) NOT NULL,
          `title` varchar(30) NOT NULL,
          `url` varchar(80) DEFAULT NULL,
          `content` blob default NULL,
          `status` tinyint(4) DEFAULT '0' COMMENT '0未处理  1处理中   2处理完成',
          PRIMARY KEY (`id`),
          KEY `bookname` (`bookname`) USING BTREE
        ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8";
        Db::execute($sql2);
        if ($page <= 15){
            $nextpage = $page + 1;
            $msg = "第{$page}页数据设置完成，正在设置第{$table}到{$nextnum}条数据";
            $this->success($msg, url('/x/ctable/' . $nextpage), '', 1);
        }
    }

    //调用命令行指令测试
    public function testConsole($id,$ided){
        $output = Console::call('getcon', [$id, $ided]);
        return $output->fetch();
    }


}